//
//  NSMChooseCityCell.m
//  EasyCar
//
//  Created by nsm on 2017/4/28.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "NSMChooseCityCell.h"

@interface ChooseCityCollectionViewCell: UICollectionViewCell
@property (nonatomic, strong) UILabel *label;
@end

@implementation ChooseCityCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _label = [UILabel new];
        _label.size = frame.size;
        _label.textColor = [UIColor darkGrayColor];
        _label.backgroundColor = [UIColor clearColor];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_label];
        
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = RGBAColor(210, 210, 210, 1).CGColor;
        
        UIView *view = [UIView new];
        view.backgroundColor = ChooseCitySelectedColor;
        self.selectedBackgroundView = view;
        
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
@end

@interface ChooseCityCollectionViewFlowLayout : UICollectionViewFlowLayout
@end

@implementation ChooseCityCollectionViewFlowLayout
- (void)prepareLayout {
    [super prepareLayout];
    
    // |-item-| 素水平方向距离屏幕间距
    CGFloat margin_h = 15.0f;
    CGFloat w = (self.collectionView.width - margin_h*2 - ChooseCityInnerSpacing*(ChooseCityMaxRowCount-1)) / ChooseCityMaxRowCount;
    self.itemSize = CGSizeMake(w, ChooseCityItemHeight);
    self.sectionInset = UIEdgeInsetsMake(0, margin_h, 0, margin_h);
    // 行间距
    self.minimumLineSpacing = ChooseCityInnerSpacing;
    // item间距
    self.minimumInteritemSpacing = ChooseCityInnerSpacing;
}
@end


@interface NSMChooseCityCell () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray <CityModel *> *cities;
@end

static NSString *identifier = @"collection_cell_identifier";
@implementation NSMChooseCityCell

- (void)setObject:(id)obj {
    if (![obj isKindOfClass:[NSArray class]]) return;
    _cities = (NSArray *)obj;
    [_collectionView reloadData];
}

- (void)setupSubviews {
    self.contentView.backgroundColor = GROUP_TABLECOLOR;
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[ChooseCityCollectionViewFlowLayout new]];
    [_collectionView registerClass:[ChooseCityCollectionViewCell class] forCellWithReuseIdentifier:identifier];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                        UIViewAutoresizingFlexibleHeight);
    _collectionView.backgroundColor = GROUP_TABLECOLOR;
    [self.contentView addSubview:_collectionView];
}

#pragma mark - UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _cities.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ChooseCityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.label.text = _cities[indexPath.row].name;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    self.actionBlock(_cities[indexPath.row]);
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

@end
